/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
 *
 * @author SONY
 */
public class CategoryDAO {
    
    DataSource ds;
    
    public CategoryDAO() {
        try {
            Context context = new InitialContext();
            ds = (DataSource) context.lookup("java:comp/env/jdbc/FashionShopDatasource");
        } catch (Exception e) {
            System.err.println(e);
        }
    }
    
    public ArrayList<Category> getAllCategories() {
        ArrayList<Category> categories = new ArrayList<Category>();
        try {
            Connection conn = ds.getConnection();
            String sql = "SELECT * FROM Category";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()) {
                String cid = rs.getString("CategoryID");
                String cname = rs.getNString("CategoryName");
                Category category = new Category(cid, cname);
                categories.add(category);
            }
        } catch(Exception e) {
            System.err.println(e);
        }
        return categories;
    }
    
}
